package com.xian;

import org.junit.Test;

import java.util.Random;

/**
 * 测试java 3种随机数
 */
public class RandomNum {

    @Test
    public void test1(){
        Random random = new Random();
        for (int i=1; i<=100; i++) {
            int num = random.nextInt();
            System.out.println("num=> "+num);
        }
    }

    @Test
    public void test2(){
        Random random = new Random(1);
        for (int i=1; i<=100; i++) {
            int num = random.nextInt();
            System.out.println("num=> "+num);
        }
    }

    /**
     * 取出随机数，然后转字符串并反转，最后看自己需要取几位数
     */
    @Test
    public void test3(){
        for (int i=1; i<=100; i++) {
            double random = Math.random(); //获取到数据如： 0.6681125840002072  18个字符
            System.out.println(random);
            String s = new StringBuilder(String.valueOf(random)).reverse().toString();
            System.out.println(s);
            String s2 = s.substring(0, 3); //从索引0开始，到索引3(包括头不包括尾)

            System.out.println("s2=> "+s2);
        }
    }


    @Test
    public void test4(){
        for (int i=1; i<=100; i++) {
            long l = System.currentTimeMillis();
            System.out.println(l);
        }
    }

    @Test
    public void test5(){
        Random random = new Random();
        for (int i=1; i<=100; i++) {
            int num1 = random.nextInt(123);
            int num2 = random.nextInt(1234567891);
            System.out.println("num=> "+num1);
            System.out.println("num2=> "+num2);
            System.out.println("nums=> "+num1+""+num2);
        }
    }

}
